package com.renwei.yunlong.activity.work;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;

import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.adapter.TablayoutAdapter;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.CommonBean;
import com.renwei.yunlong.bean.UpdateUserIconBean;
import com.renwei.yunlong.bean.WorkSpareBean;
import com.renwei.yunlong.fragment.PhotoUpdataFragment;
import com.renwei.yunlong.fragment.WorkSpareDetailFragment;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.DialogUtils;
import com.renwei.yunlong.utils.JsonMapUtil;
import com.renwei.yunlong.utils.ServiceRequestManager;
import com.renwei.yunlong.view.SimpleOptionView;
import com.renwei.yunlong.utils.CommonUtils;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class WorkSpareActivity extends BaseActivity implements View.OnClickListener, HttpTaskListener {

    @Bind(R.id.simpleTileView)
    SimpleOptionView simpleTileView;
    @Bind(R.id.tab_layout)
    TabLayout tabLayout;
    @Bind(R.id.view_pager)
    ViewPager viewPager;
    @Bind(R.id.bt_send_work)
    Button btSendWork;
    private PhotoUpdataFragment photoUpdataFragment;
    private WorkSpareDetailFragment workSpareDetailFragment;
    private String requestId;
    private String eventId;
    private WorkSpareBean workSpareBean;
    private String contractId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_work);
        ButterKnife.bind(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);

        requestId = getIntent().getStringExtra("requestId");
        workSpareBean = (WorkSpareBean) getIntent().getSerializableExtra("workSpareBean");
        eventId = getIntent().getStringExtra("eventId");
        contractId = getIntent().getStringExtra("contractId");

        initView();
    }

    private void initView() {
        simpleTileView.setTitle("备件申请");
        btSendWork.setText("操作");
        btSendWork.setOnClickListener(this);

        String[] titles = {"基本信息", "添加附件"};
        List<Fragment> list = new ArrayList<>();
        workSpareDetailFragment = new WorkSpareDetailFragment(requestId, eventId,contractId,workSpareBean);
        photoUpdataFragment = new PhotoUpdataFragment();
        list.add(workSpareDetailFragment);
        list.add(photoUpdataFragment);

        TablayoutAdapter adapter = new TablayoutAdapter(getSupportFragmentManager(), list, titles);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_send_work:
                final HashMap<String, String> map = workSpareDetailFragment.getSpareInfo();

                if ("2".equals(map.get("typeFlag"))&&"1".equals(map.get("oldReturnFlag"))){
                    DialogUtils.showDialog(this, getResources().getString(R.string.notice_work_spare), true, new DialogUtils.OnDialogClickListener() {
                        @Override
                        public void onCancel(Dialog dialog) {
                            dialog.dismiss();
                        }

                        @Override
                        public void onConfirm(Dialog dialog) {
                            setWorkSpare(map);
                            dialog.dismiss();
                        }
                    });
                }else{
                    setWorkSpare(map);
                }
                break;
                default:
                break;
        }
    }

    private void setWorkSpare(HashMap<String, String> map) {
        if (TextUtils.isEmpty(map.get("hardwareId"))) {
            showCenterInfoMsg("请选择对象");
            return;
        }
        if (TextUtils.isEmpty(map.get("spareId"))) {
            showCenterInfoMsg("请选择备件");
            return;
        }
        if (TextUtils.isEmpty(map.get("title"))) {
            showCenterInfoMsg("请输入理由");
            return;
        }

        List<UpdateUserIconBean> photos = photoUpdataFragment.getPhotoInfo();
        String filenames = "";
        String attachPath = "";
        String attachSize = "";
        for (int i = 0; i < photos.size(); i++) {
            UpdateUserIconBean photo = photos.get(i);
            if (i == 0) {
                filenames = photo.getName();
                attachPath = photo.getPath();
                attachSize = photo.getSize();
            } else {
                filenames += "," + photo.getName();
                attachPath += "," + photo.getPath();
                attachSize += "," + photo.getSize();
            }
        }
        map.put("attachPath", attachPath);
        map.put("attachSize", attachSize);
        map.put("filenames", filenames);
        if ("1".equals(companyType)) {//业主
            map.put("currentUserId", ownerBean.getRows().getEmployeeId());
        } else if ("2".equals(companyType)) {//服务商
            map.put("currentUserId", serviceLoginBean.getRows().getEmployeeId());
        }
        ServiceRequestManager.getManager().setWorkSpare(this, JsonMapUtil.mapToJson(map), this);
    }

    @Override
    public void onSuccess(int requestId, String result) {
        switch (requestId) {
            case 1:
                CommonBean bean = new Gson().fromJson(result, CommonBean.class);
                if (bean.getMessage().getCode() == 200) {
                    showCenterSuccessMsg("申请成功");
                    finish();
                } else {
                    showCenterInfoMsg(bean.getMessage().getMessage());
                }
                break;
                default:
                break;
        }
    }

    @Override    public void onException(int requestId, String code) {        showTopWrongMsg(getResources().getString(R.string.net_work_error));    }

    public static void OpenActivity(Context context, String requestId, String eventId, String contractId, WorkSpareBean workSpareBean) {
        Intent intent = new Intent(context, WorkSpareActivity.class);
        intent.putExtra("workSpareBean", workSpareBean);
        intent.putExtra("requestId", requestId);
        intent.putExtra("contractId", contractId);
        intent.putExtra("eventId", eventId);
        context.startActivity(intent);
    }
}
